<?php

include 'DBconn.php';

class CategoryMaster {

    public function getAllCategories() {
        $result = mysql_query("select * from category_header where activestatus=1") or die(mysql_error());
        $res_arr = array();
        while ($row = mysql_fetch_object($result)) {
            $tmp = new VOCategoryMaster();
            $tmp->serialnumber = (int) $row->serialnumber;
            $tmp->category_id = $row->category_id;
            $tmp->category_name = $row->category_name;
            $tmp->category_description = $row->category_description;
            $tmp->cre_userid = (int) $row->cre_userid;
            $tmp->cre_datetime = $row->cre_datetime;
            $tmp->activestatus = (boolean) $row->activestatus;
            $res_arr[] = $tmp;
        }
        mysql_free_result($result);
        return $res_arr;
    }

    public function addNewCategory($VObject) {
        $query = "insert into category_header(category_id,category_name,category_description,cre_userid,cre_datetime,activestatus) values('$VObject->category_id','$VObject->category_name','$VObject->category_description','$VObject->cre_userid','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        if ($s == 1) {
            mysql_query("COMMIT");
        } else {
            mysql_query("ROLLBACK");
        }
        return $this->getAllCategories();
    }

    public function deleteCategory($serialnumber) {
        $query = "update category_header set activestatus=0 where serialnumber='$serialnumber'";
        $s = mysql_query($query) or die(mysql_error());
        return $serialnumber;
    }

    public function editCategory($VObject) {
        $query = "update category_header set category_id='$VObject->category_id',category_name='$VObject->category_name',category_description='$VObject->category_description',cre_userid='$VObject->cre_userid',cre_datetime='$VObject->cre_datetime' where serialnumber=" . $VObject->serialnumber;
        $s = mysql_query($query) or die(mysql_error());
        if ($s == 1) {
            mysql_query("COMMIT");
        } else {
            mysql_query("ROLLBACK");
        }
        return $this->getAllCategories();
    }

    public function checkCategoryID($category_id) {
        $res = mysql_query("select * from category_header where category_id='$category_id'") or die(mysql_error());
        return mysql_num_rows($res);
    }

    public function checkCategoryName($category_name) {
        $res = mysql_query("select * from category_header where category_name ='$category_name'") or die(mysql_error());
        return mysql_num_rows($res);
    }

}

//$vo=new VOCategoryMaster;
//$vo->serialnumber=2;
//$vo->category_description="welcome";
//$vo->category_id="587642";
//$vo->cre_userid=35;
//$d=new categoryMaster;
//echo $d->editCategory($vo);
//
class VOCategoryMaster {

    var $serialnumber;
    var $category_id;
    var $category_name;
    var $category_description;
    var $cre_userid;
    var $cre_datetime;
    var $activestatus;

}

?>